# index.py

from pyjamas.ui import SimplePanel, VerticalPanel, HorizontalPanel, RootPanel
from pyjamas.ui import Label, HTML, HTMLPanel, Button, ListBox, TextBox, TextArea, Frame
from pyjamas.ui import HasAlignment
from pyjamas.ui import MenuBar, MenuItem, DialogBox
from pyjamas.HTTPRequest import HTTPRequest
from pyjamas.JSONParser import JSONParser
from pyjamas.Tooltip import Tooltip, TooltipListener
from pyjamas import DOM, Window
from Widgets import Calendar, RoundPanel, NumericTextBox

class AjaxLoader:
    def __init__(self, lblAjax = None):
        self.lblAjax = lblAjax
        if self.lblAjax:
            self.lblAjax.setVisible(True)

    def onCompletion(self, text):
        if self.lblAjax:
            self.lblAjax.setVisible(False)
        
    def onError(self, text, code):
        if self.lblAjax:
            self.lblAjax.setVisible(False)
        Window.alert("Error: " + text + "\nCodigo: " + code)

    def onTimeout(self, text):
        if self.lblAjax:
            self.lblAjax.setVisible(False)
        Window.alert("Timeout: " + text)


class HTMLLoader(AjaxLoader):
    def __init__(self, widget, lblAjax = None):
        self.widget = widget
        AjaxLoader.__init__(self, lblAjax)
        
    def onCompletion(self, text):
        AjaxLoader.onCompletion(self, text)
        self.widget.setText(text)


class JSONLoader(AjaxLoader):
    def __init__(self, func, lblAjax = None):
        self.func = func
        AjaxLoader.__init__(self, lblAjax)
        
    def onCompletion(self, text):
        AjaxLoader.onCompletion(self, text)
        self.func(text)

class MenuCmd:
    def __init__(self, object, handler):
        self._object  = object
        self._handler = handler

    def execute(self):
        handler = getattr(self._object, self._handler)
        handler()
                
class index:
    def onModuleLoad(self):
#        Window.setTitle("HELLO WORLD (Pyjamas Version)")
        html = """
        <h3 style="text-align: center;">Hello, Pyjamas World!!!</h3>
        """
        content = RoundPanel(html, bgColor = 'rgb(224,233,248)')
        DOM.setStyleAttribute(content.getElement(), "position", "relative")
        DOM.setStyleAttribute(content.getElement(), "width", "80%")
        DOM.setStyleAttribute(content.getElement(), "left", "10%")
        DOM.setStyleAttribute(content.getElement(), "top", "3em")
        RootPanel().add(content)
